package bma.common.json;

import java.io.InputStream;

import junit.framework.TestCase;

import org.codehaus.jackson.JsonNode;

import bma.common.json.impl.BeanTestObject2;
import bma.common.json.xdom.XdomJson;
import bma.common.langutil.bean.copy.BeanCopyTool;
import bma.common.langutil.core.ResourceUtil;
import bma.common.langutil.io.IOUtil;
import bma.common.langutil.xdom.Xdom;
import bma.common.langutil.xdom.XdomUtil;

public class XdomTC extends TestCase {

	protected String read(String fileName) throws Exception {
		return IOUtil.readStreamToString((InputStream) ResourceUtil
				.getResourceFromClass(XdomTC.class, fileName)
				.getContent(), "UTF-8");
	}

	public void testXdom() throws Exception {
		String json = read("json1.txt");
		JsonNode node = JsonUtil.parse(json);

		Xdom xdom = new XdomJson().bind(node);

		StringBuilder sb = new StringBuilder();
		XdomUtil.toString(xdom, sb);
		System.out.println(sb.toString());
	}

	public void testFromXdom() throws Exception {
		String json = read("json1.txt");
		JsonNode node = JsonUtil.parse(json);

		Xdom xdom = new XdomJson().bind(node);

		JsonNode r = XdomJson.fromXdom(xdom);
		System.out.println(r.toString());
	}

	public void testCopy() throws Exception {

		String json = read("object2.txt");
		JsonNode node = JsonUtil.parse(json);

		Xdom xdom = new XdomJson().bind(node);

		BeanTestObject2 obj = new BeanTestObject2();
		BeanCopyTool tool = new BeanCopyTool();

		tool.copyXdom(null, xdom, obj, null);
		System.out.println(obj);
	}
}
